package com.bd.rj.frontstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bd.rj.common.entity.User;
import com.bd.rj.common.utils.TokenUtils;
import com.bd.rj.frontstage.mapper.UserMapper;
import com.bd.rj.frontstage.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 白世俊
 * @since 2025-01-03
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
//    添加用户
    @Autowired
    UserMapper userMapper;
//    注册用户
    @Transactional
    public String register(User user){
        System.err.println("UserServiceImpl -> addUser");
//        设置创建时间为当前时间
        user.setCreatetime(new Date());
        boolean result = this.save(user);
        return result ? "添加成功" : "添加失败";
    }
    @Override
    public boolean isUsernameExists(String username) {
        // 假设UserMapper有一个方法来检查用户名是否存在
        return userMapper.equals(username);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Transactional
    public boolean updateUser(User user){
        System.err.println("UserServiceImpl -> updateUser");
        // 使用LambdaUpdateWrapper来构建更新条件
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getUsername, user.getUsername());
        // 执行更新操作
        boolean result = this.update(user, updateWrapper);
        return result;
    }

    /**
     * 校验登录内容
     * @param username
     * @param password
     * @return
     * @throws NoSuchAlgorithmException
     */
    @Override
    public Map<String, Object> login(String username, String password) throws NoSuchAlgorithmException {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        User user = userMapper.selectOne(queryWrapper);

        HashMap<String, Object> map = new HashMap<>();
        if (Objects.isNull(user)){
            map.put("code", 414);
            map.put("msg", "登录失败");
            map.put("data", "用户不存在");
            return map;
        }

        // 收集用户的所有信息
        Map<String, Object> userInfo = new HashMap<>();
        //获取用户的全部信息
        userInfo.put("id", user.getId());
        userInfo.put("username", user.getUsername());
        userInfo.put("realname", user.getRealname());
        userInfo.put("phone", user.getPhone());
        userInfo.put("email", user.getEmail());
        userInfo.put("profession", user.getProfession());
        userInfo.put("createtime", user.getCreatetime());
        userInfo.put("sex", user.getSex());
        userInfo.put("url", user.getUrl());

        // 创建Token
        String id = String.valueOf(user.getId());
        String token = TokenUtils.createToken(id, user.getUsername());
        System.out.println("token=" + token);

        // 将用户信息和Token放入返回结果中
        map.put("code", 200);
        map.put("msg", "登录成功");
        map.put("token", token);
        map.put("userInfo", userInfo); // 添加用户信息到返回值中

        return map;
    }
}
